package com.fivestyle.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fivestyle.common.lang.Result;
import com.fivestyle.common.result.R;
import com.fivestyle.entity.BannerImg;
import com.fivestyle.service.BannerImgService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.Arrays;

import java.util.List;
import java.util.UUID;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张代钊
 * @since 2021-08-04
 */
@RestController
@RequestMapping("/onlinebook/banner")
public class BannerImgController extends BaseController{

    @Autowired
    private BannerImgService bannerImgService;

//    获取前台展示的轮播图 按重要程度 从大到小 获取5张轮播图
    @ApiOperation(value = "获取轮播或者导航图",notes = "获取前台展示的轮播图 按重要程度 从大到小 获取5张轮播图")
    @GetMapping("getBannerToPre")
    public R getBannerToPre(){
        List<BannerImg> bannerList = bannerImgService.getBannerToPre();
        return R.ok().data("bannerList",bannerList);
    }



    /**
     * 后台管理
     */

    //    查询
    @GetMapping("/info/{id}")
    @PreAuthorize("hasAuthority('web:img:list')")
    public Result info(@PathVariable("id") String id) {
        BannerImg User = bannerImgService.getById(id);
        Assert.notNull(User, "找不到图片");
        return Result.succ(User);
    }

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('web:img:list')")
    public Result list(String nickname) {

        Page<BannerImg> pageData=bannerImgService.page(getPage(),new QueryWrapper<BannerImg>()
                .like(StrUtil.isNotBlank(nickname), "nickName", nickname));
        return Result.succ(pageData);
    }

    //  新增轮播图
    @Transactional
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('web:img:save')")
    public Result save(@Validated @RequestBody BannerImg Img) {



        bannerImgService.save(Img);
        return Result.succ(Img);
    }

    @Transactional
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('web:img:update')")
    public Result update(@Validated @RequestBody BannerImg Img) {
        bannerImgService.updateById(Img);
        return Result.succ(Img);
    }

    @Transactional
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('web:img:delete')")
    public Result delete(@RequestBody String[] ids) {
        bannerImgService.removeByIds(Arrays.asList(ids));
        return Result.succ("");
    }

    //    上传轮播图
    @PreAuthorize("hasAuthority('web:img:image')")
    @PostMapping("/image")
    public Result Upimage(@RequestBody MultipartFile file) throws IOException {
        String url = ossService.bannerImage(file);
        return Result.succ(url);
    }

}

